package com.cardbaobao.cardbabyclient.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.ui.activity.CreditDetailActivity;
import com.cardbaobao.cardbabyclient.ui.activity.LoansDetailActivity;
import com.cardbaobao.cardbabyclient.ui.activity.web.EventWebViewActivity;
import com.cardbaobao.cardbabyclient.ui.adapter.base.CommonRecyclerAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.base.CommonRecyclerHolder;
import com.cardbaobao.cardbabyclient.ui.adapter.base.ListenerWithPosition;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.model.IndexLayout;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;
import com.cardbaobao.cardbabyclient.widget.FlowLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Created by FHW
 * on 2017/6/13
 */

public class SpeedLoanRcAdapter extends CommonRecyclerAdapter<IndexLayout.HomeTabBean> implements ListenerWithPosition.OnClickWithPositionListener<CommonRecyclerHolder> {

    public SpeedLoanRcAdapter(Context context) {
        super(context, null, R.layout.item_speed_loan_rv);
    }

    @Override
    public void onClick(View v, int position, CommonRecyclerHolder holder) {
        ToastUtil.showToast(position + "");
    }

    @Override
    public void convert(CommonRecyclerHolder holder, final IndexLayout.HomeTabBean homeTabBean) {

        ImageLoaderUtil.displayImage(URLUtil.CBB_IMG_URL + homeTabBean.getPicture_path(), (ImageView) holder.getView(R.id.iv_pic));
        holder.getView(R.id.iv_recommend).setVisibility(View.GONE);
        holder.getView(R.id.iv_hot).setVisibility(View.GONE);
        holder.getView(R.id.iv_new).setVisibility(View.GONE);
        if (homeTabBean.getTop_tab_code_list() != null) {
            for (String s : homeTabBean.getTop_tab_code_list()) {
                switch (s) {
                    case Constant.JBQ0103020101://新
                        holder.getView(R.id.iv_new).setVisibility(View.VISIBLE);
                        break;
                    case Constant.JBQ0103020102://热
                        holder.getView(R.id.iv_hot).setVisibility(View.VISIBLE);
                        break;
                    case Constant.JBQ0103020103://荐
                        holder.getView(R.id.iv_recommend).setVisibility(View.VISIBLE);
                        break;
//                    default:
//                        holder.getView(R.id.iv_recommend).setVisibility(View.GONE);
//                        holder.getView(R.id.iv_hot).setVisibility(View.GONE);
//                        holder.getView(R.id.iv_new).setVisibility(View.GONE);
//                        break;
                }
            }
        }

        holder.setTextViewText(R.id.tv_title, homeTabBean.getTopical());
        holder.setTextViewText(R.id.tv_marketing_one, homeTabBean.getDescription());

        FlowLayout fl_label = holder.getView(R.id.fl_label);
//        String[] str = {"红包", "利率低", "高额度"};
        if (homeTabBean.getBottom_tab_code_list() != null) {
            List<String> labels=new ArrayList<>();
            for(String s:homeTabBean.getBottom_tab_code_list()){
                switch (s){
                    case Constant.JBQ0103020201:
                        labels.add("额度高");
                        break;
                    case Constant.JBQ0103020202:
                        labels.add("放款快");
                        break;
                    case Constant.JBQ0103020203:
                        labels.add("费率低");
                        break;
                    case Constant.JBQ0103020204:
                        labels.add("红包");
                        break;
                }
            }
            if(labels.size()<=3){
                fl_label.setAdapter(new FlowCommonAdapter(mContext,labels,R.layout.item_speed_loan_label_gv));
            }else{
                fl_label.setAdapter(new FlowCommonAdapter(mContext,labels.subList(0,3),R.layout.item_speed_loan_label_gv));
            }
        }
        holder.setTextViewText(R.id.tv_number, homeTabBean.getNumber());

        holder.getView(R.id.rl_item).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mIntent = new Intent();
                switch (homeTabBean.getPage_type()) {
                    case 0://原生页面
                        switch (homeTabBean.getDetail_code()) {
                            case Constant.ZCC0101://信用卡
                                mIntent.setClass(mContext, CreditDetailActivity.class);
                                mIntent.putExtra(CreditDetailActivity.CARD_ID_S,
                                        homeTabBean.getDetail_id());
                                mIntent.putExtra(CreditDetailActivity.SERIES_CARD_ID_S,
                                        homeTabBean.getDetail_id());
                                mIntent.putExtra(CreditDetailActivity.CARD_NAME_S,
                                        homeTabBean.getTopical());
                                mIntent.putExtra(CreditDetailActivity.FROM_PAGE_S,
                                        "首页");
                                mContext.startActivity(mIntent);
                                break;
                            case Constant.ZCC0102://贷款
                                mIntent.setClass(mContext, LoansDetailActivity.class);
                                mIntent.putExtra(LoansDetailActivity.LOANS_ID_S,
                                        homeTabBean.getDetail_id());
                                mIntent.putExtra(LoansDetailActivity.LOANS_NAME_S,
                                        homeTabBean.getTopical());
                                mIntent.putExtra(LoansDetailActivity.FROM_PAGE_S,
                                        "首页");
                                mContext.startActivity(mIntent);
                                break;
                        }
                        break;
                    case 1://H5页面上传
                    case 2://
                        mIntent.setClass(mContext, EventWebViewActivity.class);
                        mIntent.putExtra("url", homeTabBean.getUrl());
                        mIntent.putExtra(EventWebViewActivity.WEB_TITLE_S, homeTabBean.getTopical());
                        mIntent.putExtra(EventWebViewActivity.WEB_TYPE_I, 10);
                        mContext.startActivity(mIntent);
                        break;
                }
            }
        });

    }

}
